class Admin::MemberController < Admin::BaseController
  
  before_filter :get_users, :only => [:index]
  before_filter :get_user_ranks, :only => [:member_rank]
  before_filter :get_admins, :only => [:admin_list]
  
  def index
    respond_to do |format|
      format.html
      format.js { 
        render :update do |page|
          page.replace_html 'list', :partial => 'list' 
        end
      }
    end
  end
  
  def edit
    @user = User.find_by_id(params[:id])
  end
  
  verify :method => :post, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
  def update
    @user = User.find_by_id(params[:id])
    @user.attributes = params[:user]
    if @user.valid? && @user.save!
      respond_to do |format|
        format.html { redirect_to :action => :index }
      end
    else
      flash[:error] = t(:a_upd_lose)
      respond_to do |format|
        format.html { render :action => :edit }
      end
    end
  end
  
  def lock
    user = User.find_by_id(params[:id])
    render :update do |page|
      return if user.blank?
      user.locked = !user.locked
      user.save(false)
      src = user.locked ? "/images/admin/unlock.gif" : "/images/admin/lock.gif"
      page << "$('img_#{params[:id]}').src = '#{src}';"
    end
  end
  
  verify :method => [:post, :delete], :only => :destroy, :render => { :nothing => true, :status => :method_not_allowed }
  def destroy
    user = User.find_by_id(params[:id])
    if user.present?
      begin
        user.reload.destroy
        flash[:notice] = t(:delete_success)
      rescue ::ActiveRecord::RecordNotFound
      end
    else
      flash[:notice] = t(:delete_failed)
    end
    get_users
    respond_to do |format|
      format.js { 
        render :update do |page|
          page.replace_html 'list', :partial => 'list' 
        end
      }
    end
  end
  
  def csv_export
    
  end
  
  def csv_import
    
  end
  
  def member_rank
    respond_to do |format|
      format.html
      format.js { 
        render :update do |page|
          page.replace_html 'list', :partial => 'rank_list' 
        end
      }
    end
  end
  
  verify :method => :post, :only => :create_member_rank, :render => {:nothing => true, :status => :method_not_allowed }
  def create_member_rank
    @user_rank = UserRank.new(params[:user_rank])
    if @user_rank.valid? && @user_rank.save!
      respond_to do |format|
        format.html { redirect_to :action => :member_rank }
      end
    else
      respond_to do |format|
        format.html { render :action => :add_member_rank }
      end
    end
  end
  
  def edit_member_rank
    @user_rank = UserRank.find_by_id(params[:id])
  end
  
  verify :method => :post, :only => :update_member_rank, :render => {:nothing => true, :status => :method_not_allowed }
  def update_member_rank
    @user_rank = UserRank.find_by_id(params[:id])
    @user_rank.attributes = params[:user_rank]
    if @user_rank.valid? && @user_rank.save!
      respond_to do |format|
        format.html { redirect_to :action => :member_rank }
      end
    else
      flash[:error] = t(:a_upd_lose)
      respond_to do |format|
        format.html { render :action => :edit_member_rank }
      end
    end
  end
  
  verify :method => [:post, :delete], :only => :destroy_member_rank, :render => { :nothing => true, :status => :method_not_allowed }
  def destroy_member_rank
    user_rank = UserRank.find_by_id(params[:id])
    if user_rank.present?
      begin
        user_rank.reload.destroy
        flash[:notice] = t(:delete_success)
      rescue ::ActiveRecord::RecordNotFound
      end
    else
      flash[:notice] = t(:delete_failed)
    end
    get_user_ranks
    respond_to do |format|
      format.js { 
        render :update do |page|
          page.replace_html 'list', :partial => 'rank_list' 
        end
      }
    end
  end
  
  def admin_list
    respond_to do |format|
      format.html
      format.js { 
        render :update do |page|
          page.replace_html 'list', :partial => 'admin_list' 
        end
      }
    end
  end

  verify :method => :post, :only => :create_admin, :render => {:nothing => true, :status => :method_not_allowed }
  def create_admin
    @admin = Admin.new(params[:admin])
    if @admin.valid? && @admin.save!
      respond_to do |format|
        format.html { redirect_to :action => :admin_list }
      end
    else
      respond_to do |format|
        format.html { render :action => :add_admin }
      end
    end
  end

  def edit_admin
    @admin = Admin.find_by_id(params[:id])
  end
  
  verify :method => :post, :only => :update_admin, :render => {:nothing => true, :status => :method_not_allowed }
  def update_admin
    @admin = Admin.find_by_id(params[:id])
    @admin.attributes = params[:admin]
    if @admin.valid? && @admin.save!
      respond_to do |format|
        format.html { redirect_to :action => :admin_list }
      end
    else
      flash[:error] = t(:a_upd_lose)
      respond_to do |format|
        format.html { render :action => :edit_admin }
      end
    end
  end

  verify :method => [:post, :delete], :only => :destroy_admin, :render => { :nothing => true, :status => :method_not_allowed }
  def destroy_admin
    admin = Admin.find_by_id(params[:id])
    if admin.present?
      begin
        admin.reload.destroy
        flash[:notice] = t(:delete_success)
      rescue ::ActiveRecord::RecordNotFound
      end
    else
      flash[:notice] = t(:delete_failed)
    end
    get_admins
    respond_to do |format|
      format.js { 
        render :update do |page|
          page.replace_html 'list', :partial => 'admin_list' 
        end
      }
    end
  end

  private
  
  def get_users
    @users = User.paginate(:all, :page => params[:page], :per_page => 10)
  end
  
  def get_user_ranks
    @user_ranks = UserRank.paginate(:page => params[:page], :per_page => 10)
  end
  
  def get_admins
    @admins = Admin.paginate(:all, :page => params[:page], :per_page => 10)
  end
  
end
